<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('基金估值') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('估值时间')" prop="valuationTime">
        <DatePicker
          v-model="valuationTimes"
          type="daterange"
        />

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:valuation:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :v-bind:v-has-permi="['fund:valuation:import']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >{{ $t('导入') }}</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:valuation:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="fundValuationList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('基金名称')" align="center" prop="fundName" min-width="200">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.fundName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('估值时间')" align="center" prop="valuationTime" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.valuationTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('估值金额') + $t('金额单位')" align="center" prop="valuationAmount" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.valuationAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已投项目个数')" align="center" prop="investedItemNum" min-width="120" />
      <el-table-column :label="$t('实际募资规模') + $t('金额单位')" align="center" prop="fundraisingScale" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.fundraisingScale, 'w') }}</span>
        </template>
      </el-table-column>w
      <el-table-column :label="$t('已投规模') + $t('金额单位')" align="center" prop="investScale" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.investScale, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('剩余规模') + $t('金额单位')" align="center" prop="surplusScale" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.surplusScale, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['fund:valuation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['fund:valuation:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <div v-loading="upload.isUploading">
        <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">
            {{ $t('将文件拖到此处，或') }}
            <em>{{ $t('点击上传') }}</em>
          </div>
          <div slot="tip" class="el-upload__tip" style="color:red">{{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}</div>
        </el-upload>
        <div slot="footer" class="dialog-footer" style="position: relative;text-align: right;">
          <el-button type="info" class="el-icon-download el-icon--right" style="font-size:12px;float:left;" @click="fetchTemplate">
            {{ $t('下载模板') }}
          </el-button>
          <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="$refs.upload.submit(); upload.loading = true">{{ $t("确定") }}</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 添加或修改基金估值对话框 -->
    <el-dialog :title="title" :visible.sync="formOpen" :destroy-on-close="true" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <fundValuationForm ref="fundValuationForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listFundValuation, delFundValuation } from '@/api/fund/valuation/fundValuation'
import fundValuationForm from './fundValuationForm.vue'
import { downLoadExcel } from '@/utils/excelDownload'
import { getToken } from '@/utils/auth'
export default {
    components: {
        fundValuationForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 基金估值表格数据
          fundValuationList: [],
          // 弹出层标题
          title: '',
          // 是否显示弹出层
          formOpen: false,
          // 估值时间查询参数
          valuationTimes: undefined,
          // 查询参数
          queryParams: {
              fundId: this.$route.query.id,
              startDate: undefined,
              endDate: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          // 导入参数
          upload: {
            // 是否显示弹出层
            open: false,
            // 弹出层标题
            title: '',
            // 是否禁用上传
            isUploading: false,
            // 是否更新已经存在的用户数据
            updateSupport: 0,
            dealId: this.$route.query.id,
            // 设置上传的请求头部
            headers: { Authorization: 'Bearer ' + getToken() },
            // 上传的地址
            url: process.env.VUE_APP_BASE_API + '/valuation/fundValuation/importData'
          }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询基金估值列表 */
    getList() {
        this.loading = true
        if (this.valuationTimes != null && this.valuationTimes.length > 1) {
          this.queryParams.startDate = this.valuationTimes[0]
          this.queryParams.endDate = this.valuationTimes[1]
        } else {
          this.queryParams.startDate = undefined
          this.queryParams.endDate = undefined
        }
        listFundValuation(this.queryParams).then(response => {
            this.fundValuationList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            fundId: undefined,
            investedItemNum: undefined,
            fundraisingScale: undefined,
            investScale: undefined,
            fundCost: undefined,
            surplusScale: undefined,
            valuationTime: undefined,
            valuationAmount: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.valuationTimes = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('添加基金估值')
        this.$nextTick(() => {
            this.$refs.fundValuationForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('修改基金估值')
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.fundValuationForm.init(row)
        })
    },
    /** 导入 **/
    handleImport() {
      this.upload.title = this.$t('导入')
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 下载模板
    fetchTemplate() {
      var href1 = window.location.href
      var href2 = this.$route.path
      var url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/valuationOfManagedFunds.xlsx'
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delFundValuation(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        downLoadExcel('/valuation/fundValuation/export', this.queryParams)
    }
  }
}
</script>
